﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
//using CCBRSA;
using Beway.ValueAdded.Util;
using Beway.ValueAdded.Service;
using Beway.ValueAdded.Model;


namespace Beway.ValueAdded.RechargeGateway.Controllers
{
    public class PaymentController : Controller
    {        
        private string ReturnParam = "MERCHANTID={0}&ORDERID={1}&PAYMENT={2}&BJOURNAL={3}&SUCCESS={4}"; 
        string _REMARK1 = "";
        string _REMARK2 = "";
        string _ORDERID;
        string _PAYMENT;
        string _SUCCESS = "";
        string log = "";
        RechargeServ reserv = new RechargeServ();
        /// <summary>
        /// 支付结果接收
        /// </summary>
        /// <returns></returns>
        public ActionResult PayReturn()
        {            
            string _POSID = Request["POSID"];
            string _BRANCHID = Request["BRANCHID"];
            string _MERCHANTID = Request["MERCHANTID"];
            _ORDERID = Request["ORDERID"];
            _PAYMENT = Request["PAYMENT"];         
            _REMARK1 = Request["REMARK1"];
            _REMARK2 = Request["REMARK2"];
            string _BJOURNAL=Request["BJOURNAL"];
            _SUCCESS = Request["SUCCESS"];
            string _SIGNBANK = Request["SIGNBANK"];
            ReturnParam = string.Format(ReturnParam, _MERCHANTID, _ORDERID, _PAYMENT, _BJOURNAL, _SUCCESS);

            VerifyPay(_SIGNBANK,ReturnParam);

            return View();
        }

        /// <summary>
        /// 验证签名
        /// </summary>
        /// <param name="retparam"></param>
        /// <param name="sign"></param>
        private void VerifyPay(string sign,string retparam)
        {
            //RSASig rsa;
            //rsa = new CCBRSA.RSASigClass();
            bool bRet;

            //设置公钥
            //rsa.setPublicKey(Config.PUBLICKEY);

            //验签
            bRet = true;//rsa.verifySigature(sign, retparam);
            if (bRet && _SUCCESS == "Y")
            {
                ViewBag.OrderID = _ORDERID;
                //更新订单状态, 根据订单号，金额，未支付三个条件，在事务中查询记录，防止重复更新，导致重复充值 0（已更新，或者金额不符，或没有此订单），1更新成功，可以后续充值
                int ret = reserv.PayStatusUpdate(_ORDERID, decimal.Parse(_PAYMENT));
                if (ret == 1)//更新支付状态成功，可以充值               
                {
                    Recharge re = reserv.GetById(int.Parse(_REMARK1));
                    ReUser ru = new ReUser();                    
                    string rechargeResult = reserv.RechargeTrade(re,ru);//欧飞返回码 0充值中, 1成功, 9充值失败，可以退款, 11运营商维护，暂不能充值
                 
                    if (rechargeResult == "1")
                    {                     
                        ViewBag.RechargeResult = "充值成功！";
                    }                   
                    else if (rechargeResult == "0")
                    {                         
                        ViewBag.RechargeResult = "正在充值中，请留意手机短信提醒。";
                    }
                    else if(rechargeResult =="9") //9充值失败，此情况都可以退款                    
                    {                       
                        ViewBag.RechargeResult = "充值失败，请联系客服！";
                    }
                    else if (rechargeResult == "11")//运营商缴护，暂不能充值
                    {
                        ViewBag.RechargeResult = "运营商维护，暂不能充值，请联系客服！";
                    }

                    //更新充值结果
                    reserv.Update(re);
                } 
                else if(ret==2)//之前已收到支付结果，正在充值中。。
                {
                    ViewBag.RechargeResult = "正在充值中，请留意手机短信提醒。";
                }
                else if (ret == 3)//已经充值成功
                {
                    ViewBag.RechargeResult = "充值成功！";
                }
                else//没有查到交易记录或存储过程执行失败
                {
                    ViewBag.RechargeResult = "支付结果异常，请查询银行账号是否已经扣款，如已扣款请联系客服！";
                }
            }
            else //验签或者支付失败
            {
                ViewBag.RechargeResult = "支付失败，请查询银行账号是否已经扣款，如已扣款请联系客服！";
            }

            ////写日志
            //try
            //{
            //    Util.StringUtil.WriteStringToFile(Server.MapPath(this.Request.ApplicationPath) + "paylog.txt", log);
            //}
            //catch
            //{ }
        }

    }
}
